 
function removeTask(obj) {
    var that = $(obj);
    var tid = that.data("id");
    var pid = that.data("pid");
    if(tid == 0){
        that.remove();
    }else{
        var pids = tid;
        if (pid == 0) {
            $("#task_list [data-pid=" + tid + "]").each(function(){
                pids += "," + $(this).data("id");
            });
        }
        $("#pids").val(pids);
        $.ajax({
            url: '',
            type: 'post',
            dataType: 'json',
            data:{
                'pids':pids
            },
            cache: false,
            success: function (data) {
                if (data.flag == 1) {
                    if (data.count > 0) {
                        $(".remove_taolun").text("该任务下已有输出文档，不可删除！");
                        $("#delButton").hide();
                    } else {
                        $(".remove_taolun").text("该任务下的所有文档和评论将同时删除。如果您完成了这个任务，通常选择将该任务标记为已完成状态，而不是删除。您是否确定删除？");
                    }
                    $("#remove_man").modalopen();
                }
            },
            error: function (msg) {
                //测试

                that.remove();
            }
        });
    }
}

   function addTemplateSubTask(obj) {
    var html = $("#tmpSubTask").find("tr").eq(0).html();
    var ptask = $(obj);
    var pid = ptask.data("id");
    var pmajor = ptask.data("major");
    var psub = ptask.data("sub");
    var pstage = ptask.data("stage");
    html = '<tr class="sub-task" edit_flag="1" data-id="0" data-pid="'+pid+'" data-stage="'+pstage+'">' + html + '</tr>';
    var subTasks = $("#task_list [data-pid=" + pid + "]");
    var insertObj;
    if (subTasks.length > 0) {
        insertObj = $("#task_list [data-pid=" + pid + "]:last");
        insertObj.after(html);
    } else {
        insertObj = $("#task_list [data-id=" + pid + "]");
        insertObj.after(html);
    }
    $("#pid_0").val(pid);
    $("#periodId_0").val(pstage);
    $("#subId_0").val(psub);
    $("#majorId_0").val(pmajor);
    $("#task_list [edit_flag=1]").find("input:first")[0].focus();

}


function delTask(obj){
    var pids = $(obj).next().next().val();
    $.ajax({
        url:'',
        type:'post',
        dataType:'json',
        data:{"pids":pids,"type":"projectTask"},
        cache:false,
        success:function(data) {
            if(data.flag == 1){
                //$("#remove_man").modalclose();
                //var arr = pids.split(",");
                //$.each(arr,function(key,val){
                //    $("#task_list [data-id=" + val + "]").remove();
                //});
                window.location.reload();
            }else{
                alert("删除任务失败！！！");
            }
        },
        error:function(msg) {
          
        }
    });
}


function filter(obj,type,val){
    param = '';
    if(type=='more'){
        var isCheck = $(obj).attr("checked");
        if($(obj).parent().attr("id")=='task-stage'){

            if(isCheck!=undefined){
                $(obj).removeAttr("checked");
            }else{
                $(obj).attr("checked","checked");
            }
            $(obj).siblings().each(function () {
                $(this).removeAttr("checked");
            });

            var that = $(obj).find("a").eq(0);
            stages = that.eq(0).data("id");
            if(stages == -11){
                stages = '';
            }

            selectTaskStage(that);


            $("#periodId").val(stages);
            var txt = that.text();
            $("#periodId").next().children().first().text(txt);
            $("#periodId").next().next().find("li").each(function(){
                if(that.attr("data")==stages){
                    that.find("a").eq(0).attr("class","selected");
                }
            });

            // add by tony 20160811
            var id = $(obj).find('a').data('id');
            if(id == '-1'){
                $('#task_list tr').show();
            }else{
                $('#task_list tr').hide();
                $('#task_list tr.stages_'+id).show();
            }
            // end add

        }else{
            if(isCheck!=undefined){
                $(obj).removeAttr("checked");
            }else{
                $(obj).attr("checked","checked");
            }
        }
    }else if(type=='member'){
        if($(obj).children().first().hasClass("selected")){
            projectMember = 0;
        }else{
            projectMember = val;
        }

    }else if(type=='projectStatus'){
        var flag = false;//任务筛选是否选中 默认为未选中
        if(val != 6 && val != 7){
            $("#taskStatus").css("visibility","hidden");
        }else{
            $("#taskStatus").removeAttr("style");
        }
        var isCheck = $(obj).attr("checked");

        if(isCheck!=undefined){
            $(obj).removeAttr("checked");
        }else{
            $(obj).attr("checked","checked");
        }

        $(obj).siblings().each(function () {
            $(this).removeAttr("checked");
        });
        $("#prostatus li").each(function () {
            if ($(this).attr("checked") == "checked") {
                flag = true;
                projectStatus = $(this).attr("data");
            }
        });
        if($("input[type='checkbox']").is(':checked') && flag){
            projectStatus = 8;
        }else if($("input[type='checkbox']").is(':checked') && !flag){
            projectStatus = 7;
        }else if(!$("input[type='checkbox']").is(':checked') && !flag){
            projectStatus = 0;
        }else if(!$("input[type='checkbox']").is(':checked') && flag){
            projectStatus = projectStatus;
        }
    }else if(type=='sort'){
        sortColumn=val;
        sortMethod=$(obj).attr("data-sortmethod");
    }

    keyword=$("#keyword").val();
    $("#start").val(0);
    var  pass_ul_flag = false;
    var  pass_ul2_flag = false;
    $.each($("#pass_ul li"),function(i,u){
        if($(u).attr("checked")=="checked"){
            pass_ul_flag = true;
            subIds += $(u).attr("data")+",";
        }
    });
    $.each($("#pass_ul2 li"),function(i,u){
        if($(u).attr("checked")=="checked"){
            pass_ul2_flag = true;
            majors += $(u).attr("data")+",";
        }
    });

    if(!pass_ul_flag){
        $("#projectSub").text('').text('全部');
    }
    if(!pass_ul2_flag){
        $("#major").text('').text('全部');
    }


}


function addSubTaskCharge(obj,userId,taskCharge){
    var index = $(obj).parent().prev().attr("id").split("_")[1];
    var picurl = $(obj).attr("picurl");
    $("#taskChargeName_"+index).val(taskCharge);
    $("#userId_"+index).val(userId);
    $(obj).parent().parent().prev().attr("style","background:url('"+picurl+"') no-repeat scroll 0 12px;padding-left: 15px;background-size:12px,15px; ");
}
function selectTaskStage(stageObj) {
    var sid = $(stageObj).data("id");
    var stxt = $(stageObj).text();
    $(stageObj).parents("ul").children().removeClass("selected");
    $(stageObj).parents("ul").find(".icon-remove2").hide();
    $(stageObj).parents("li").addClass("selected");
    $(stageObj).parents("li").find(".icon-remove2").show();
}
function add_xm_js(obj){ 
     //var sum=$(".xiangmu_mb").find("li").eq(index-1).find("input:text").length;
        var sum=$("#xiangmu_mb_cont").find("input:text").length;
        
     for(var i=0;i<sum;i++){ 
        //$("#xiangmu_mb_cont").find("input:text").eq(i).next().css("display","none");
        $("#xiangmu_mb_cont").find("input:text").eq(i).parent().css("position","static"); 
     }
     $(obj).parent().find(".fudong_add_td").css("display","block");
     $(obj).parent().css("position","relative");
     
}
function taskStageSort(){
    var ids = '';
    $.each($("#task-stage").find("li"),function(i,u){
        if($(u).find("a").eq(0).data("id")>0){
            ids += $(u).find("a").eq(0).data("id") + ",";
        }
    });
    $.ajax({
        url:'',
        type:'post',
        dataType:'json',
        data:{},
        cache:false,
        success:function(data) {
        },
        error:function(msg) {
          
        }
    });
}


function selectTemplateTaskStage(stageObj) {
    var sid = $(stageObj).data("id");
    var stxt = $(stageObj).text();
    $("#task-percent").parent().find("label").html(stxt + " 的阶段");
    $(stageObj).parents("ul").children().removeClass("selected");
    var aa = $(stageObj).parents("li");
    $(stageObj).parents("li").addClass("selected");
}

function addTemplateTaskStageById(sid, sname) {
    if ($("#task-stage [data-id=" + sid + "]").length == 0) {
                    taskStageSort();
                    var html = '<li onclick="filter(this,\'more\');"><a data-id="' + sid + '" title="'+sname+'" style="padding-top:5px;"><div class="content-2line" style="font-size: 14px;">' + sname + '</div></a><span class="icon-remove2" onclick="deleteTaskStage(this);"></span></li>';
                    $("#task-stage li:last").after(html);
                    selectTemplateTaskStage($("#task-stage a:last"));
                    stages = sid;
                    tmpStages = sid;
                    $("#projectStage").val("");
                    queryData();
        $.ajax({
            url: '',
            type: 'post',
            dataType: 'json',
            data:{
  
            },
            cache: false,
            success: function (data) {
                if(data.flag == 1){

                }else{
                    alert("添加失败");
                }
            },
            error: function (msg) {
             alert(msg)
            }
        });
    }
}

function addTemplateTaskStage(obj) {
    var sid = $(obj).data("id");
    var sname = $(obj).text();
    addTemplateTaskStageById(sid, sname);
    $(obj).parents(".input_dowm").hide();
}


//删除标签
  function deleteTaskStage(obj){
            var $this = $(obj);
            var taskStage = $this.prev().data("id");
            $.ajax({
                type : "POST",
                url : "",
                data : {
      
                },
                async : false,
                dataType : "json",
                success : function(data) {
                    if(data.flag == 1){
                        $("#periodId").parent().find("li [data="+taskStage+"]").remove();
                        $("#task-stage").find("li").removeClass("selected");
                        $this.parent().prev().attr("class","selected");
                        var id = $this.parent().prev().find("a").eq(0).data("id");
                        $("#periodId").val(id);
                        $this.parent().remove();
                    }
                }
            });
        }

function init(){
    $('[data-toggle="Task_onc"]').click(function (e) {
        $(this).information('Task',{
            onFa:false,
            eles:'add_text',
            List:'add_con_list',
            elem:'Task-t'
        });
        e.stopPropagation();
    });

    $(".Screening_Singular").find("li").on({
        click:function(e){
            $(this).information('Task_list',{
                onFa:true,
                intext:'types',
                elem:'choose_class'

            });
            e.stopPropagation();
        }
    });

    $(document).on('click',function(e){
        var ofs=$(e.target).parents().hasClass("fudong_add_td");
        var ofg=$(e.target).hasClass("fudong_add_td");
        var osg=$(e.target).hasClass("Template_name");
        if(!osg) {
            if(ofs || ofg){
            }
            else{
                $(".fudong_add_td").hide();
            }
        }else{
            $(".fudong_add_td").hide();
            $(e.target).parent().find(".fudong_add_td").show();
        }
    });

    $(document).on("click",".task-input",function(){
        $(this).information('zhunag',{
            elem:'task-input-ul',
            eles:'task-input-ms'
        });
    });

    $(document).on('click',function(e){
        var ofs=$(e.target).parents().hasClass("bb_con");
        var ofg=$(e.target).hasClass("bb_con");
        var osg=$(e.target).hasClass("shows-pak");
        if(!osg) {
            if(ofs || ofg){

            }
            else{
                $(".bb_con").hide();
            }
        }
    });
}
function queryData(){
    $.ajax({
        url:'',
        type:'post',
        dataType:'text',
        data:{},
        cache:false,
        success:function(data) {
            $(".task-list").empty();
            $(".task-list").html(data);
            init();
        },
        error:function(msg) {
          
        }
    });
}



//更新任务状态
function changeStatus(status,id,majorId){
    $.ajax({
        url:'',
        type:'post',
        dataType:'json',
        data:{},
        cache:false,
        success:function(data) {
            if(data.flag==1){
                var par = $("#task_list [data-id="+id+"]").data("parent");
                if(status == 4){
                    $("#task_list [data-id="+id+"]").find("td:not('.add-sub-task')").each(function(i){
                        if(i==0 && par == 0){
                            $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);padding-left:0;");
                            $(this).find("a").attr("style","color: rgb(204,204,204);");
                        }else if(i == 0 && par == 1){
                            $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                            $(this).find("a").attr("style","color: rgb(204,204,204);");
                        }else{
                            $(this).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                            $(this).find("input").eq(0).attr("style","text-decoration: line-through; color: rgb(204, 204, 204);");
                        }
                    });
                    var mydate = getNowFormatDate();
                    $("#task_list [data-id="+id+"]").find("td").last().prev().prev().text(mydate);
                }else{
                    $("#task_list [data-id="+id+"]").find("td").each(function(i){
                        if(i == 0 && par == 0){
                            $(this).attr("style","padding-left:0;");
                            $(this).find("a").attr("style","");
                        }else if(i == 0 && par == 1){
                            $(this).attr("style","");
                            $(this).find("a").attr("style","");
                        }else{
                            $(this).attr("style","");
                            $(this).find("input").eq(0).attr("style","");
                        }

                    });
                    $("#task_list [data-id="+id+"]").find("td:last").prev().prev().text('');
                }

                init();
            }else{
                alert("更新状态失败！！！")
            }
        },
        error:function(msg) {
        
        }
    });

}


function createSubTask() {
    var title = $.trim($("#task_list [edit_flag=1]").find("input").eq(0).val());
    if (title == '') {
        alert("子任务名称必填");
    } else {
        $.ajax({
            type: "POST",
            url: '',
            data: $('#createSubTaskForm').serialize(),
            async: false,
            dataType: "json",
            cache: false,
            success: function (data) {
                if (data.flag == 1) {
                    var periodId = $("#periodId_0").val();
                    var majorId = $("#majorId_0").val();
                    var taskChargeName = $("#taskChargeName_0").val();
                    var expectFinishDate = $("#expectFinishDate_0").val();
                    var html = '<tr data-parent="1" class="sub-task" data-stage="' + periodId + '" data-id="' + data.id + '" data-pid="' + data.pid + '">';
                    html += '<td colspan="2"><div class="task-input-ms f_l mt5"><div type="text" class="task-input shows-pak"><img src="' + pic + 'ta2.png" class="shows-pak"/></div>';
                    html += '<ul class="task-input-ul bb_con" style="display: none;">';
                    html += '<li data="' + pic + 'ta2.png" onclick="changeStatus(2,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta2.png"/>待做</li>';
                    html += '<li data="' + pic + 'ta1.png" onclick="changeStatus(1,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta1.png"/>进行中</li>';
                    html += '<li data="' + pic + 'ta3.png" onclick="changeStatus(3,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta3.png"/>暂停</li>';
                    html += '<li data="' + pic + 'ta4.png" onclick="changeStatus(4,' + data.id + ',' + majorId + ')"><img src="' + pic + 'ta4.png"/>已完成</li></ul></div>';
                    html += '<div class="ml40 content-2line" style="display: block;margin-top: 5px;" title="' + data.no + ' / ' + title + '"><a data-toggle="modal-task" data-target="#taskPanel" data-url="' + web_path + 'project/task/detail/' + orgId + '/' + projectId + '/' + data.pid + '">' + data.no + '</a>&nbsp;/&nbsp';
                    html += '<a data-toggle="modal-task" data-target="#taskPanel" data-url="' + web_path + 'project/task/detail/' + orgId + '/' + projectId + '/' + data.id + '">' + title + '</a></div></td>';
                    html += '<td><div class="content-2line" style="display: block" title="' + taskChargeName + '">' + taskChargeName + '</div></td><td></td>';
                    html += '<td>' + expectFinishDate + '</td>';
                    html += '<td><p class="icon-remove2 hide"></p></td></tr>';
                    $("#task_list [edit_flag=1]").before(html);
                   /* $("#task_list [edit_flag=1]").focus();*/
                    $("#task_list [edit_flag=1]").remove();
                    //获取父任务在数组中的位置
                    var len = $("#task_list [data-pid="+data.pid+"]").length;
                    var index = $.inArray(data.pid.toString(),taskIdArr);
                    var tmpSubTaskId = data.id;
                    //将新增的子任务id添加到数组中父任务对应位置的后面
                    taskIdArr.splice(index+len,0,tmpSubTaskId);
                    $(window).focus();
                } else {
                    alert("创建失败")
                }
            }
        });
    }
}

/**
 * 隐藏删除按钮
 * @param obj
 */
function hide_tag(obj){
    $(obj).find("input[type='button']").css('visibility','hidden');
}

/**
 * 显示删除按钮
 * @param obj
 */
function remove_tag(obj){
    $(obj).find("input[type='button']").css('visibility','visible');
}

function foldEve(){
     // clickThis点击当前元素的class ，foldChild为折叠区域的class,外面用foldEve包起来
    $(".clickThis").mouseover(function(){
        $(this).parents(".foldEve").find(".foldChild").addClass("showTrsm");    
         return false;//阻止受点击document事件的影响
    });
    $(".clickThis").mouseout(function(){
        $(".foldChild").removeClass("showTrsm");
        return false;//阻止受点击document事件的影响
    });
    //鼠标松开,展开区域折叠
    $(document).click(function(){
        $(".foldChild").removeClass("showTrsm");
    }); 
}
//模拟复选框
function checkBoxInput(){
    $(".checkBox").click(function(){
        $(this).find("em").toggleClass("selectstyle");            
    })
}

/**
 * 左侧模块选中状态
 */
function selectLeftMenu(){
    var url = window.location.pathname;
    //console.log("<<<< url:" + url);
    if(url.indexOf("/comset/addressbook.htm")==0) {
        //员工
        $(".leftSide .icon6 a").addClass("current");
    }else if(url.indexOf("/comset")==0 || url.indexOf("/project/set")==0) {
        //公司设置
        $(".leftSide .setIcon").addClass("current-set-icon");
    }else if (url.indexOf("/project")==0) {
        //项目
        $(".leftSide .icon1 a").addClass("current");
    }else if(url.indexOf("/thread")==0) {
        //线索
        $(".leftSide .icon2 a").addClass("current");
    }else if(url.indexOf("/opportunity")==0) {
        //机会
        $(".leftSide .icon3 a").addClass("current");
    }else if(url.indexOf("/contract")==0) {
        //合同
        $(".leftSide .icon4 a").addClass("current");
    }else if(url.indexOf("/customer")==0) {
        //客户
        $(".leftSide .icon5 a").addClass("current");
    }else if(url.indexOf("/daily/stat/")==0) {
        //统计
        $(".leftSide .icon7 a").addClass("current");
    }else if(url.indexOf("/user/panel/index.htm")==0) {
        $(".headerOrder ul li.desk a").addClass("current");
    }else if(url.indexOf("/report/index.htm")==0) {
        $(".headerOrder ul li.report a").addClass("current");
    }else if(url.indexOf("/calendar/index.htm")==0) {
        $(".headerOrder ul li.calendar a").addClass("current");
    }else if(url.indexOf("/company/index.htm")==0){
        $(".leftSide .icon100 a").addClass("current");
    }else if(url.indexOf("/promanage/index.htm")==0){
        $(".leftSide .icon200 a").addClass("current");
    }else if(url.indexOf("/user/index.htm")==0){
        $(".leftSide .icon300 a").addClass("current");
    }else if(url.indexOf("/mail")==0){
        $(".leftSide .icon400 a").addClass("current");
    }

}

$(document).ready(function(){
    selectLeftMenu();
    var main_left = $("#main-left");
    var main_right = $("#main-right");
    var max_height = $(window).height()- 58 - 50 - 10;
    if (main_left.length>0 && main_left.outerHeight(true)<max_height){
        main_left.css("height",max_height + "px");
    }

    if (main_right.length>0 && main_right.outerHeight(true)<max_height){
        main_right.css("height",max_height + "px");
    }
});
